//
//  FindViewController.m
//  SugarPile
//
//  Created by Charlie. on 16/1/18.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "FindViewController.h"
#import "FindModel.h"
#import "LCPrefixHeader.pch"
#import "FindTableViewCell.h"
#import "FindDetailsViewController.h"
#import "PopCollectionViewCell.h"
#import "SeekViewController.h"
#import "PopDetailsViewController.h"
#import "PopMoreViewController.h"
@interface FindViewController ()<UITableViewDataSource, UITableViewDelegate, passDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UITableView *findTableView; /**< 主要tableView*/
@property (nonatomic, retain) NSMutableArray *findArr; /**< 目录数据数组*/
@property (nonatomic, retain) NSMutableArray *findNameArr;/**<名字数据数组*/
@property (nonatomic, retain) UICollectionView *popcollection; /**< 流行趋势的collection*/
@property (nonatomic, retain) NSMutableArray *popArr; /**< 流行趋势数据数组 */
@property (nonatomic, retain) UIView *header; /**<tableView头高*/

@end

@implementation FindViewController

- (void)dealloc
{
    [_findTableView release];
    [_findArr release];
    [_findNameArr release];
    [_popcollection release];
    [_popArr release];
    [_header release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(right)] autorelease];
    
    // 创建TableView
    [self createTableView];
    
    // 解析数据
    [self dataHandle];
    
    // 创建tableView头高
    self.header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, W, 200 * H)];
//    _header.backgroundColor = [UIColor saddleBrown];
    [self.view addSubview:_header];
    _findTableView.tableHeaderView = _header;
    [self.header release];
    // 标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10 * W, 10 * H, 200 * W, 20 * H)];
    title.text = @"流行趋势";
    title.textColor = [UIColor lightGrayColor];
    [self.header addSubview:title];
    [title sizeToFit];
    title.font = [UIFont systemFontOfSize:11];
    [title release];
//    // 创建Collection
    [self createCollection];
}
- (void)right
{
    SeekViewController *seekVC = [[SeekViewController alloc] init];
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:seekVC];
    [self presentViewController:nc animated:YES completion:^{
        
    }];

}
- (void)dataHandle
{
    [LCAFNetWorking GETWithURL:@"http://203.80.144.212/napi/index/groups/?platform_version=4.4.4&device_platform=HM%2B2A&screen_width=720&screen_height=1280&__domain=www.duitang.com&app_version=67&platform_name=Android&locale=zh&app_code=nayutas" completion:^(id result) {
        NSArray *arr = result[@"data"];
        // 总数据的第一个数据放入流行数组
        self.popArr = [FindModel modelHanderWithArray: arr[0][@"group_items"]] ;
        
        self.findArr = [NSMutableArray array];
        self.findNameArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            // 解析出标题数组
            // 所有标题
            [self.findArr addObject:dic[@"group_name"]];
            // 解析出名字数组
            // 所有跳转名字数组
            NSMutableArray *arr2 = [FindModel modelHanderWithArray:dic[@"group_items"]];
            [self.findNameArr addObject:arr2];
            }
        
        [_popcollection reloadData];
        [_findTableView reloadData];
    }];
}
// collectionView
- (void)createCollection
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(80 * W, 100 * H);
    layout.minimumInteritemSpacing = 5 * H;
    layout.minimumLineSpacing = 30 * W;
    layout.sectionInset = UIEdgeInsetsMake(10 * H, 10 * W, 10 * H, 10 * W);
    self.popcollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40 * H, WIDTH, 120 * H) collectionViewLayout:layout];
    _popcollection.backgroundColor = [UIColor floralWhite];
    _popcollection.delegate = self;
    _popcollection.dataSource = self;
    // 隐藏滚动条
    _popcollection.showsHorizontalScrollIndicator = NO;
    // 边缘弹动
    _popcollection.bounces = NO;
    [_popcollection registerClass:[PopCollectionViewCell class] forCellWithReuseIdentifier:@"popcell"];
    [self.header addSubview:_popcollection];
    [self.popcollection release];

}
// tablleView
- (void)createTableView
{
    // 创建
    self.findTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _findTableView.delegate = self;
    _findTableView.dataSource = self;
    // 边缘弹动
    _findTableView.bounces = NO;
    
    [self.view addSubview:_findTableView];
    [_findTableView registerClass:[FindTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.findTableView release];
    
}
#pragma mark - table协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor floralWhite];
    cell.delegate =self;
// 进行判断 进行传值
    if (indexPath.section == 0) {
        cell.findArr = _findNameArr[1];
        
    }
    if (indexPath.section == 1) {
        cell.findArr = _findNameArr[2];
        
    }
    if (indexPath.section == 2) {
        cell.findArr = _findNameArr[3];
        
    }
    if (indexPath.section == 3) {
        cell.findArr = _findNameArr[4];
        
    }
    if (indexPath.section == 4) {
        cell.findArr = _findNameArr[5];
        
    }
 return cell;
}


#pragma mark - collection协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _popArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"popcell" forIndexPath:indexPath];
    FindModel *m = _popArr[indexPath.item];
    
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:m.icon_url]];
//    NSLog(@"%@", m.icon_url);
    cell.nameLabel.text = m.name;
    [cell.nameLabel sizeToFit];
    cell.nameLabel.numberOfLines = 0;
    return cell;
}
// 点击传值
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindModel *m = _popArr[indexPath.item];
    NSArray *arr = [m.target componentsSeparatedByString:@"&"];
    PopDetailsViewController *popVC = [[[PopDetailsViewController alloc] init] autorelease];
    // push 时隐藏底部栏
    popVC.hidesBottomBarWhenPushed = YES;
    // 将分隔后的数组数据进行传值
    if (indexPath.row < 7) {
        if (arr.count == 3) {
            popVC.popId = [arr[1] stringByReplacingOccurrencesOfString:@"theme_alias=" withString:@""];
            if (indexPath.row == 1) {
                popVC.popId = [@"%E5%BD%B1%E9%9F%B3%E4%B9%A6_" stringByAppendingString:popVC.popId];
            }else if(indexPath.row == 2)
            {
                popVC.popId = [@"%E7%BE%8E%E9%A3%9F%E8%8F%9C%E8%B0%B1_" stringByAppendingString:popVC.popId];
            }
        }
        if (arr.count == 4) {
            popVC.popWebURL = [arr[3] stringByReplacingOccurrencesOfString:@"target=" withString:@""];
            NSLog(@"%@", popVC.popWebURL);
        }
        
        [self.navigationController pushViewController:popVC animated:YES];
    }
    if (indexPath.row == 7) {
        PopMoreViewController *popVC = [[[PopMoreViewController alloc] init] autorelease];
        //
        [self.navigationController pushViewController:popVC animated:YES];
    }

}
#pragma mark - 协议传值 方法
-(void)tianzhuanWithstring:(NSString *)str
{
    // 跳转
    NSString *urlId = [str substringFromIndex:49];
    NSLog(@"%@", urlId);
    FindDetailsViewController *findVC = [[[FindDetailsViewController alloc] init] autorelease];
    findVC.str = urlId;
    // push 时隐藏底部栏
    findVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:findVC animated:YES];
    
}

// 设置分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 设置自定义头部
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    header.backgroundColor = [UIColor yellowColor];
    // 设置自定以头部字体
    UILabel *nameHeader = [[UILabel alloc] initWithFrame:CGRectMake(10 * W, 5 * H, 70 * W, 20 * H)];
    [header addSubview:nameHeader];
    nameHeader.textColor = [UIColor whiteColor];
    nameHeader.layer.cornerRadius = 20;
    nameHeader.clipsToBounds = YES;
    // 居中
    nameHeader.textAlignment = NSTextAlignmentCenter;
    nameHeader.font = [UIFont systemFontOfSize:11];
   // 设置线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(80 * W, 15 * H, WIDTH - 80 * W, 1)];
    [header addSubview:line];
    line.backgroundColor = [UIColor orangeColor];
    [nameHeader release];
    [line release];
    // 设置标题头
    if (section == 0) {
        nameHeader.text = _findArr[1];
         nameHeader.backgroundColor = [UIColor orangeColor];
        line.backgroundColor = [UIColor orangeColor];
    }
    if (section == 1) {
        nameHeader.text = _findArr[2];
         nameHeader.backgroundColor = [UIColor blueViolet];
        line.backgroundColor = [UIColor blueViolet];
    }
    if (section == 2) {
        nameHeader.text = _findArr[3];
         nameHeader.backgroundColor = [UIColor lawnGreen];
        line.backgroundColor = [UIColor lawnGreen];
    }
    if (section == 3) {
        nameHeader.text = _findArr[4];
         nameHeader.backgroundColor = [UIColor gambogeColor];
        line.backgroundColor = [UIColor gambogeColor];
    }
    if (section == 4) {
        nameHeader.text = _findArr[5];
         nameHeader.backgroundColor = [UIColor doderBlue];
        line.backgroundColor = [UIColor doderBlue];
    }

    return [header autorelease];
}

// 分区头名称 header
-(UIView *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return _findArr[section + 1];
}

// 分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * H;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
